package com.dhb.pattern.proxy.dynamicproxy.myproxy;

import java.lang.reflect.Method;

/**
 * 功能描述：
 * 创建人：citymoon
 * 创建日期：2019/5/12
 */
public class DHBDynamicProxy implements DHBInvocationHandler {

    private Object target;

    public Object getInstance(Object target) throws Exception{
        this.target = target;
        Class<?> clazz = target.getClass();
        return DHBProxy.newProxyInstance(new DHBClassLoader(), clazz.getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object object = method.invoke(this.target, args);
        after();
        return object;
    }

    private void before() {
        System.out.println("自我介绍");
    }

    private void after(){
        System.out.println("感谢观众");
    }
}
